package org.cycuchy.hp.app {
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.containers.Canvas;
	import mx.core.Application;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;
	import mx.managers.ToolTipManager;
	
	import org.cycuchy.hp.components.HPImgTitleWindow;
	import org.cycuchy.hp.components.book.HPBook;
	import org.cycuchy.hp.components.menu.HPMenu;
	import org.cycuchy.hp.components.player.HPPlayer;
	import org.cycuchy.hp.data.EmPeCzysData;
	import org.cycuchy.hp.data.FotosData;
	import org.cycuchy.hp.data.KapstelkiData;
	import org.cycuchy.hp.event.XMLParsedEvent;

	public class HPApplication extends Application {
		
		//public var mainCanvas:Canvas;
		public var menuCanvas:Canvas;
		public var player:HPPlayer;
//		public var viewer:Landscape;
		
		
//		public var pl:HPPreloader;
		public var buk:HPBook;
//		private var fotos:FotosData;
//		private var empeczys:EmPeCzysData;
		
		private var tw:HPImgTitleWindow;
		
		public function HPApplication():void {
			super();
			addEventListener(FlexEvent.PREINITIALIZE, preinit);
//			addEventListener(FlexEvent.INITIALIZE, init);
			addEventListener(FlexEvent.CREATION_COMPLETE, creationComplete);
			addEventListener(Event.COMPLETE, complete);
		}
		
		override protected function createChildren():void {
			super.createChildren();
			menuCanvas.addChild(new HPMenu([
				{text1: "ZACZYNAMY", text2: "Strona Główna", clickCallback: gotoFrontPage}, 
				{text1: "O NAS", text2: "Cycuchy", clickCallback: gotoCycuchyPage},
				{text1: "MIEJSCA", text2: "Tu Nas Szukaj", clickCallback: gotoMapPage},
				{text1: "HOBBY", text2: "Po Godzinach", clickCallback: gotoHobbyPage},
				{text1: "FOTKI", text2: "Zdjęcia", clickCallback: gotoFotkiPage},
				{text1: "ZDARZENIA", text2: "Działo Się", clickCallback: gotoMiejscowkiPage},
			]));
		}		
		
		private function preinit(event:FlexEvent):void {
//			trace("HPApplication::preinit");
			removeEventListener(FlexEvent.PREINITIALIZE, preinit);
			addEventListener(XMLParsedEvent.KAPSTELIKI_DONE, kapselkiXMLParsed);
//			addEventListener(XMLParsedEvent.KAPSTELKI_ERROR, kapselkiXMLError);
			addEventListener(XMLParsedEvent.FOTOS_DONE, fotosXMLParsed);
			addEventListener(XMLParsedEvent.EMPECZY_DONE, empeczyXMLParsed);
//			addEventListener(XMLParsedEvent.FOTOS_ERROR, fotosXMLError);
		}
		
//		private function init(event:FlexEvent):void {
//			trace("HPApplication::init");
//		}
		
		private function creationComplete(event:FlexEvent):void {
//			trace("HPApplication::creationComplete");
			removeEventListener(FlexEvent.CREATION_COMPLETE, creationComplete);
			ToolTipManager.showDelay = 100;
		}
		
		private function complete(event:Event):void {
//			trace("HPApplication::complete");
			removeEventListener(Event.COMPLETE, complete);
			player.play(null);
			buk.visible = true;
			menuCanvas.visible = true;
			player.visible = true;
		}
		
		private function kapselkiXMLParsed(event:XMLParsedEvent):void {
//			trace("HPApplication::kapselkiXMLParsed: " + event.object);
			removeEventListener(XMLParsedEvent.KAPSTELIKI_DONE, kapselkiXMLParsed);
			buk.kapstelkiBrowserManager.datas = KapstelkiData(event.object); 
		}
		
//		private function kapselkiXMLError(event:XMLParsedEvent):void {
//			trace("HPApplication::kapselkiXMLError");
//			addBook();
//		}
		
		private function fotosXMLParsed(event:XMLParsedEvent):void {
//			trace("HPApplication::kapselkiXMLParsed: " + event.object);
			removeEventListener(XMLParsedEvent.FOTOS_DONE, fotosXMLParsed);
			buk.fotosBrowserManager.datas = FotosData(event.object);
//			fotos = FotosData(event.object);
//			addBook(kapstelki, fotos);
		}
		
		private function empeczyXMLParsed(event:XMLParsedEvent):void {
//			trace("HPApplication::empeczyXMLParsed: " + event.object);
			removeEventListener(XMLParsedEvent.EMPECZY_DONE, empeczyXMLParsed);			
			player.empeczys = EmPeCzysData(event.object);
		}
		
		
//		private function fotosXMLError(event:XMLParsedEvent):void {
//			trace("HPApplication::kapselkiXMLError");
//			addBook();
//		}
		
//		private function addBook(kapsteliki:KapstelkiData=null, fotos:FotosData=null):void {
//			book = new HPBook();
//			book.visible = false;
//			book.kapstelikiData = kapsteliki;
//			book.fotosData = fotos;
//			mainCanvas.addChild(book);
//		}

//		private function getBuk():HPBook {
//			return buk;
//		}
		
		private function gotoFrontPage(event:Event):void {
			buk.gotoFrontPage(event);
		}		 
		
		private function gotoCycuchyPage(event:Event):void {
			buk.gotoCycuchyPage(event);
		}		 
		
		private function gotoMapPage(event:Event):void {
			buk.gotoMapPage(event);
		}		 
		
		private function gotoHobbyPage(event:Event):void {
			buk.gotoHobbyPage(event);
		}		 
		
		private function gotoFotkiPage(event:Event):void {
			buk.gotoFotkiPage(event);
		}
		
		private function gotoMiejscowkiPage(event:Event):void {
			buk.gotoMiejscowkiPage(event);
		}
		
//		private var image:Image;
		
		public function showTitleWindow(target:DisplayObject, id:uint):void {
			trace("stw");
//			image = new Image();
			tw = HPImgTitleWindow(PopUpManager.createPopUp(buk, HPImgTitleWindow, true));
			var data:BitmapData = new BitmapData(target.width, target.height, true, 0);
			data.draw(target);
			var bitmap:Bitmap = new Bitmap(data);			
			tw.img.source = bitmap;
			tw.imgId = id;
			PopUpManager.centerPopUp(tw);
		}
		
//		private function onComplete(event:Event):void {
//			tw = HPImgTitleWindow(PopUpManager.createPopUp(book, HPImgTitleWindow, true));
//			tw.img.source = Bitmap(image.content);
//			PopUpManager.centerPopUp(tw);			
//		}
		
		public function hideTitleWindow():void {
			PopUpManager.removePopUp(tw);
			tw = null;
		}
		
	}
	
}